package edu.uit.se.slgroup.construct.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import edu.uit.se.slgroup.construct.domain.SubCategory;
import edu.uit.se.slgroup.construct.domain.SubCategoryTemp;

public class SubCategoryDaoImpl implements SubCategoryDao{

	@Autowired
	SessionFactory sessionFactory;
	
	@Transactional
	public int insertSubCategory(SubCategory subCategory) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(subCategory);
		tx.commit();
		Serializable id = session.getIdentifier(subCategory);
		session.close();
		return (Integer) id;
	}

	public List<SubCategory> getSubCategoryList() {
		Session session = sessionFactory.openSession();
		@SuppressWarnings("unchecked")		
		List<SubCategory> subCategoryList = session.createQuery(
				"from SubCategory").list();		
		session.close();
		return subCategoryList;
	}

	public SubCategory getSubCategoryById(int id) {
		Session session = sessionFactory.openSession();
		SubCategory subCategory = (SubCategory) session.load(SubCategory.class, id);
		//dong ket noi, nhieu truong hop khong lay duoc obj
		//session.close();
		return subCategory;
	}

	public int updateSubCategory(SubCategory subCategory) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(subCategory);
		tx.commit();
		Serializable id = session.getIdentifier(subCategory);
		session.close();
		return (Integer) id;
	}

	public int deleteSubCategory(int id) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		SubCategory subCategory = (SubCategory) session.load(SubCategory.class, id);
		session.delete(subCategory);
		tx.commit();
		Serializable ids = session.getIdentifier(subCategory);
		session.close();
		return (Integer) ids;
	}

	public List<SubCategoryTemp> getSubCategoryTempList() {
		Session session = sessionFactory.openSession();
		@SuppressWarnings("unchecked")	 

		List<SubCategoryTemp> subCategoryTempList = new ArrayList<SubCategoryTemp>();
		String SQL_QUERY = "Select SubCategory.id, SubCategory.name, Category.name"
				+ " from edu.uit.se.slgroup.construct.domain.SubCategory SubCategory, " 
				+ "edu.uit.se.slgroup.construct.domain.Category Category " 
				+ "where SubCategory.idCategory = Category.id order by SubCategory.id asc";
		Query query = session.createQuery(SQL_QUERY);
		List result = query.list();
		Iterator itr= result.iterator();
		while(itr.hasNext()) {
			Object[] row = (Object[]) itr.next();
			SubCategoryTemp sct = new SubCategoryTemp();
			sct.setId((Integer) row[0]);
			sct.setName((String) row[1]);
			sct.setIdCategory((String) row[2]);
			subCategoryTempList.add(sct);			
		}
		
		for (SubCategoryTemp object : subCategoryTempList) {
			System.out.println("ID: " + object.getId());
			System.out.println("Name: " + object.getName());
			System.out.println("ID Category: " + object.getIdCategory());
		}	
		
		session.close();
		return subCategoryTempList;
	}

}
